package com.fastwire.carriere.service;

import java.util.List;

import javax.jws.WebService;

import com.fastwire.carriere.common.Messages;
import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.common.util.ResourceUtil;
import com.fastwire.carriere.common.util.StringUtil;
import com.fastwire.carriere.controller.ControllerProxy;
import com.fastwire.carriere.controller.FDFrController;
import com.fastwire.carriere.controller.impl.FDFrControllerImpl;
import com.fastwire.carriere.inventory.vo.ConnectivityVo;
import com.fastwire.carriere.inventory.vo.FDFrVo;
import com.fastwire.carriere.service.dataconverter.XMLConverter;
import com.fastwire.carriere.service.webui.exception.ServiceException;
import com.fastwire.carriere.user.vo.SessionVo;

// TODO: Auto-generated Javadoc
/**
 * The Class FDFrInventoryServiceImpl.
 */
@WebService(endpointInterface = "com.fastwire.carriere.service.FDFrInventoryService")
public class FDFrInventoryServiceImpl implements FDFrInventoryService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.service.FDFrInventoryService#
	 * getServiceGraphbyNativeName(java.lang.String)
	 */
	@Override
	public String getServiceGraphbyNativeName(String fdfrNativeEmsName,
			SessionVo sessionVo) throws ServiceException {

		CommonLogger
				.info(this, "getE2EGraphbyNativeName, " + fdfrNativeEmsName);
		try {

			FDFrController fdfrController = (FDFrController) ResourceUtil
					.getSessionBindClassByName(
							FDFrControllerImpl.class.getName(), sessionVo,
							false);

			ConnectivityVo connVo = fdfrController
					.stitchFDFrService(fdfrNativeEmsName);
			
			// added by Jun on 20120509
			List<ConnectivityVo> connList = fdfrController.stitchRad(connVo);
			
			//return XMLConverter.serilizeObjectToXML(connVo);
			return XMLConverter.serilizeObjectListToXML(connList);

		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.FDFrInventoryService#getFDFrsbyNeId(java
	 * .lang.String)
	 */
	@Override
	public List<FDFrVo> getFDFrsbyNeId(String neId, SessionVo sessionVo)
			throws ServiceException {

		try {
			FDFrController fdfrController = (FDFrController) ResourceUtil
					.getSessionBindClassByName(
							FDFrControllerImpl.class.getName(), sessionVo, true);
			List<FDFrVo> fdFrVos = fdfrController.getFdFrsbyNE(neId);

			return fdFrVos;
		} catch (Exception e) {
			e.printStackTrace();
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.service.FDFrInventoryService#
	 * getFDFrbyNativeEmsNameAndNeId(java.lang.String, java.lang.String)
	 */
	@Override
	public FDFrVo getFDFrbyNativeEmsNameAndNeId(String nativeEmsName,
			String neId, SessionVo sessionVo) throws ServiceException {

		CommonLogger.info(this,
				"getFDFrbyNativeEmsNameAndNeId, emsNativeName: "
						+ nativeEmsName + ",neID:" + neId);

		try {

			FDFrController fdfrController = (FDFrController) ResourceUtil
					.getSessionBindClassByName(
							FDFrControllerImpl.class.getName(), sessionVo, true);
			FDFrVo fdfrVo = fdfrController.getFdFrbyNativeEmsNameAndNeId(
					nativeEmsName, neId);
			return fdfrVo;
		} catch (Exception e) {
			e.printStackTrace();
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.FDFrInventoryService#getFDFRsbyTunnelName
	 * (java.lang.String)
	 */
	@Override
	public List<FDFrVo> getFDFRsbyTunnelName(String tunnelName,
			SessionVo sessionVo) throws ServiceException {

		try {
			FDFrController fdfrController = (FDFrController) ResourceUtil
					.getSessionBindClassByName(
							FDFrControllerImpl.class.getName(), sessionVo, true);
			List<FDFrVo> fdFrVos = fdfrController
					.getFDFrsbyTunnelName(tunnelName);

			return fdFrVos;

		} catch (Exception e) {
			e.printStackTrace();
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.FDFrInventoryService#getFDFrsbySearchCriteria
	 * (com.fastwire.carriere.inventory.vo.FDFrVo)
	 */
	@Override
	public List<String> getFDFrNamesbySearchCriteria(FDFrVo searchVo,
			SessionVo sessionVo) throws ServiceException {

		try {
			FDFrController fdfrController = (FDFrController) ResourceUtil
					.getSessionBindClassByName(
							FDFrControllerImpl.class.getName(), sessionVo, true);
			return fdfrController.searchFDFr(searchVo);

		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.service.FDFrInventoryService#
	 * getFDFrPMPGraphbyNeIdAndPTPPort(java.lang.String, java.lang.String)
	 */
	@Override
	public String getFDFrPMPGraphbyNeIdAndPTPPort(String neId, String ptp,
			SessionVo sessionVo) throws ServiceException {

		try {

			FDFrController fdfrController = (FDFrController) ResourceUtil
					.getSessionBindClassByName(
							FDFrControllerImpl.class.getName(), sessionVo,
							false);
			List<ConnectivityVo> connectivityVos = fdfrController
					.getPMPFDFRGrouphbyNE(neId, ptp);

			String connInfo = XMLConverter
					.serilizeObjectListToXML(connectivityVos);
			return connInfo;

		} catch (Exception e) {

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}
	}

	@Override
	public String updateFDFr(FDFrVo fdFrVo, SessionVo sessionVo)
			throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		if (StringUtil.isEmptyString(fdFrVo.getRemarks())) {
			return Messages.getString("message.common.error.nullempty",
					new String[] { "Remark" });
		}

		if (StringUtil.isEmptyString(fdFrVo.getNativeEmsName())) {
			return Messages.getString("message.common.error.nullempty",
					new String[] { "FDFr Native EMS Name" });
		}

		try {

			FDFrController fdfrController = (FDFrController) ResourceUtil
					.getSessionBindClassByName(
							FDFrControllerImpl.class.getName(), sessionVo, true);
			fdfrController.updateFDFr(fdFrVo);
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

		return msg;

	}

}
